package net.xiaoyu233.mitemod.miteite.trans.entity;

import net.minecraft.*;
import net.xiaoyu233.mitemod.miteite.util.Configs;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.SoftOverride;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.Random;

@Mixin(EntityCow.class)
public abstract class EntityCowTrans extends EntityLivestockTrans {

    public EntityCowTrans(World world) {
        super(world);
    }

    @Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/PathfinderGoalSelector;addTask(ILnet/minecraft/PathfinderGoal;)V", ordinal = 7))
    private void redrectEntityCow(PathfinderGoalSelector instance, int par1, PathfinderGoal par2EntityAIBase) {
        instance.addTask(1, new PathfinderGoalMeleeAttack(this, EntityPlayer.class, 1.4F, true));
        this.targetTasks.addTask(1, new PathfinderGoalHurtByTarget(this, true));
        this.targetTasks.addTask(2, new PathfinderGoalNearestAttackableTarget(this, EntityPlayer.class, 0, true));
    }

    @SoftOverride
    public int getBreedExp() {
        return Configs.wenscConfig.breedXpCow.ConfigValue;
    }
//    public boolean onEntityRightClicked(EntityPlayer player, ItemStack item_stack) {
//        if (super.onEntityRightClicked(player, item_stack)) {
//            return true;
//        } else if (this.riddenByEntity == null) {
//            if (player.onServer()) {
//                player.mountEntity(this);
//            }
//
//            return true;
//        } else {
//            return false;
//        }
//    }


//    public void moveEntityWithHeading(float par1, float par2) {
//        if (this.riddenByEntity != null) {
//            this.prevRotationYaw = this.rotationYaw = this.riddenByEntity.rotationYaw;
//            this.rotationPitch = this.riddenByEntity.rotationPitch * 0.5F;
//            this.setRotation(this.rotationYaw, this.rotationPitch);
//            this.rotationYawHead = this.renderYawOffset = this.rotationYaw;
//            par1 = ((EntityLiving) this.riddenByEntity).moveStrafing * 0.5F;
//            par2 = ((EntityLiving) this.riddenByEntity).moveForward;
//            if (par2 <= 0.0F) {
//                par2 *= 0.25F;
//            }
//
//
//            this.stepHeight = 1.0F;
//            this.jumpMovementFactor = this.getAIMoveSpeed() * 0.1F;
//            if (!this.worldObj.isRemote) {
//                this.setAIMoveSpeed((float) this.getEntityAttribute(GenericAttributes.movementSpeed).getAttributeValue());
//                super.moveEntityWithHeading(par1, par2);
//            }
//
//            this.prevLimbSwingAmount = this.limbSwingAmount;
//            double var8 = this.posX - this.prevPosX;
//            double var5 = this.posZ - this.prevPosZ;
//            float var7 = MathHelper.sqrt_double(var8 * var8 + var5 * var5) * 4.0F;
//            if (var7 > 1.0F) {
//                var7 = 1.0F;
//            }
//
//            this.limbSwingAmount += (var7 - this.limbSwingAmount) * 0.4F;
//            this.limbSwing += this.limbSwingAmount;
//        } else {
//            this.stepHeight = 0.5F;
//            this.jumpMovementFactor = 0.02F;
//            super.moveEntityWithHeading(par1, par2);
//        }
//
//    }

    @Override
    protected void applyEntityAttributes() {
        int day = this.getWorld() != null ? Math.max(this.getWorld().getDayOfOverworld() - 20, 0) : 0;
        this.setEntityAttribute(GenericAttributes.followRange, 12.0F);
        this.setEntityAttribute(GenericAttributes.movementSpeed, 0.23F);
        this.setEntityAttribute(GenericAttributes.attackDamage, (double) 0.5F + (double) day / (double) 32.0F);
        this.setEntityAttribute(GenericAttributes.maxHealth, (double) 6.0F + (double) day / (double) 10f);
        super.applyEntityAttributes();
    }

    @Override
    public EntityDamageResult attackEntityAsMob(Entity target) {
        if (this.isDecoy()) {
            return null;
        } else if (target instanceof EntityPlayer && target.getAsPlayer().isImmuneByGrace()) {
            return null;
        } else {
            DamageSource var10002 = DamageSource.causeMobDamage(this);
            Damage damage = new Damage(var10002, 0.5f);
            if (this.isFrenzied()) {
                damage.addAmount((float) this.getEntityAttributeBaseValue(GenericAttributes.attackDamage));
            }

            int knockback_bonus = 0;
            if (target.isEntityLivingBase()) {
                knockback_bonus += EnchantmentManager.getKnockbackModifier(this, target.getAsEntityLivingBase());
            }

            int fire_aspect = EnchantmentManager.getFireAspectModifier(this);
            EntityDamageResult result = target.attackEntityFrom(damage.setFireAspect(fire_aspect > 0));
            if (result == null) {
                return result;
            } else {
                if (result.entityWasNegativelyAffected()) {
                    if (knockback_bonus > 0) {
                        target.addVelocity(-MathHelper.sin(this.rotationYaw * (float) Math.PI / 180.0F) * (float) knockback_bonus * 0.5F, 0.1, MathHelper.cos(this.rotationYaw * (float) Math.PI / 180.0F) * (float) knockback_bonus * 0.5F);
                        this.motionX *= 0.6;
                        this.motionZ *= 0.6;
                    }
                    if (this.isBurning() && !this.hasHeldItem() && this.rand.nextFloat() < (float) this.worldObj.difficultySetting * 0.3F) {
                        target.setFire(2 * this.worldObj.difficultySetting);
                    }
                    if (target instanceof EntityPlayer) {
                        this.refreshDespawnCounter(-9600);
                    }
                }
                return result;
            }
        }
    }

    @Override
    public EntityDamageResult attackEntityFrom(Damage damage) {
        if (damage.getSource().damageType.equals("player") || damage.getSource().damageType.equals("mob")) {
            if (damage.getSource().getResponsibleEntity() instanceof EntityPlayer) {
                EntityPlayer player = ((EntityPlayer) damage.getSource().getResponsibleEntity());
                this.setAttackTarget(player);
            }
        }
        return super.attackEntityFrom(damage);
    }

    @Redirect(method = "dropFewItems", at = @At(value = "INVOKE", target = "Ljava/util/Random;nextInt(I)I", ordinal = 1))
    private int randLightValue(Random random, int lightValue) {
        return random.nextInt(4) + 2;
    }
}
